<template>
  <Category :scene="scene"></Category>
  <el-card style="margin: 10px 0">
    <!-- 展示数据 -->
    <div v-show="scene == 0">
      <el-button
        @click="changeToSpu"
        type="primary"
        size="default"
        icon="Plus"
        :disabled="!categoryStore.C3Id"
      >
        添加SPU属性
      </el-button>
      <!-- 表格展示数据 -->
      <el-table style="margin: 10px 0" border :data="records">
        <el-table-column
          type="index"
          label="序号"
          width="80px"
          align="center"
        />
        <el-table-column label="SPU名称" prop="spuName" />
        <el-table-column label="SPU描述" show-overflow-tooltip="true">
          <template #="{ row }">
            {{ row.description }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #="{ row }">
            <el-button
              type="primary"
              size="small"
              icon="Plus"
              title="添加SKU"
              @click="changeToSku(row)"
            ></el-button>
            <el-button
              type="primary"
              size="small"
              icon="Edit"
              title="修改SPU"
              @click="editSpu(row)"
            ></el-button>
            <el-button
              type="primary"
              size="small"
              icon="View"
              title="查看SKU列表"
              @click="viewSku(row)"
            ></el-button>
            <el-popconfirm
              :title="`你确定删除${row.spuName}?`"
              width="200px"
              @confirm="deleteSpu(row)"
            >
              <template #reference>
                <el-button
                  type="primary"
                  size="small"
                  icon="Delete"
                  title="删除SPU"
                ></el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        @size-change="changeSize"
        :pager-count="8"
        @current-change="getHasSpu"
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[3, 5, 7, 9]"
        :background="true"
        layout="prev, pager, next, jumper, ->,sizes, total"
        :total="total"
      />
    </div>
    <spuForm ref="spu" v-show="scene == 1" @back="backToShow"></spuForm>
    <skuForm ref="sku" v-show="scene == 2" @back="backToShow"></skuForm>

    <!-- dialog对话框:展示已有的SKU数据 -->
    <el-dialog v-model="dialogShow" title="SKU列表">
      <el-table border :data="skuArr">
        <el-table-column label="SKU名字" prop="skuName"></el-table-column>
        <el-table-column label="SKU价格" prop="price"></el-table-column>
        <el-table-column label="SKU重量" prop="weight"></el-table-column>
        <el-table-column label="SKU图片">
          <template #="{ row }">
            <img :src="row.skuDefaultImg" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </el-card>
</template>

<script setup lang="ts">
import { ref, watch, onBeforeUnmount } from 'vue'
import type {
  spuResponseData,
  spuRecords,
  spuData,
  SkuData,
  SkuInfoData,
} from '@/api/product/type'
import { reqHasSpu, reqSkuList, reqRemoveSpu } from '@/api/product/spu/index'
import useCategoryStore from '@/store/modules/category'
import skuForm from './skuForm.vue'
import spuForm from './spuForm.vue'
import { ElMessage } from 'element-plus'
let scene = ref<number>(0)
let currentPage = ref<number>(1)
let pageSize = ref<number>(3)
let categoryStore = useCategoryStore()
let records = ref<spuRecords>([])
let total = ref<number>(0)
let spu = ref<any>(undefined)
let sku = ref<any>(undefined)

let dialogShow = ref<boolean>(false)
let skuArr = ref<SkuData[]>([])
//监听第三级分类的变化获取数据显示到card上
watch(
  () => categoryStore.C3Id,
  () => {
    //获取数据
    if (categoryStore.C3Id) getHasSpu()
  },
)
//获取Spu数据的方法
const getHasSpu = async () => {
  let result: spuResponseData = await reqHasSpu(
    currentPage.value,
    pageSize.value,
    categoryStore.C3Id,
  )
  if (result.code == 200) {
    records.value = result.data.records
    total.value = result.data.total
  }
}
//更改一页显示的数量
const changeSize = () => {
  currentPage.value = 1
  getHasSpu()
}

//切换场景到SPU
const changeToSpu = () => {
  scene.value = 1
  //调用子组件初始化Spu数据编辑界面
  spu.value.initAddSpu(categoryStore.C3Id)
}

//切换场景到SKU
const changeToSku = (row: spuData) => {
  //点击添加SKU按钮切换场景为2
  scene.value = 2
  //调用子组件的方法初始化添加SKU的数据
  sku.value.initSkuData(categoryStore.C1Id, categoryStore.C2Id, row)
}
//从Spu编辑场景切回数据展示场景
const backToShow = async (obj: any) => {
  scene.value = obj.num
  if (obj.flag == 1) {
    currentPage.value = 1
  }
  //从编辑场景切回来的时候数据已经更新  需要重新获取
  await getHasSpu()
}
//切换到编辑Spu的场景
const editSpu = (row: spuData) => {
  scene.value = 1
  spu.value.initHasSpuData(row)
}

//查看某一SPU的SKU列表
const viewSku = async (row: spuData) => {
  let result: SkuInfoData = await reqSkuList(row.id as number)
  if (result.code == 200) {
    skuArr.value = result.data
    //对话框显示出来
    dialogShow.value = true
  }
}

const deleteSpu = async (row: spuData) => {
  let result: any = await reqRemoveSpu(row.id as number)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    //获取剩余SPU数据
    if (records.value.length == 1 && currentPage.value != 1)
      currentPage.value = currentPage.value - 1
    getHasSpu()
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败',
    })
  }
}

//销毁之前清空仓库中的数据，防止切换到其他模块时数据依然存在
onBeforeUnmount(() => {
  categoryStore.$reset()
})
</script>

<style scoped></style>
